package com.leoman.spinningmachine.tab2;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.util.RefreshUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.base.BaseFragment;
import com.leoman.spinningmachine.bean.MsgBean;
import com.leoman.spinningmachine.bean.MyMessageBean;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.main.MainActivity;
import com.leoman.spinningmachine.tab1.LeaveSpActivity;
import com.leoman.spinningmachine.tab1.QytzActivity;
import com.leoman.spinningmachine.tab3.LeaveActivity;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 消息
 */
public class MsgFragment extends BaseFragment implements BaseRecyclerAdapter.onItemClickListener {

    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.fl_search)
    FrameLayout fl_search;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.bgaRefreshLayout)
    BGARefreshLayout bgaRefreshLayout;

    private MsgAdapter adapter;
    private List<MsgBean> list = new ArrayList<>();
    private List<MyMessageBean> msgLists = new ArrayList<>();
    private String[] titles = {"打卡通知：邵纺机智慧科技园", "审批人：迪丽热巴", "邵纺机智慧科技园", "请假人：周二珂"};
    private String[] contents = {"[考勤打卡]2018-03-29 08:30 邵纺机智慧科技园", "[请假审批]与领导合作！", "[企业通知]新款产品上市了！", "[请假审批]准备粉丝见面会！"};

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fg_msg;
    }

    @Override
    protected void initView(View view) {
        initSearch();

        ((BaseActivity) getActivity()).setLayoutManager(recyclerView, LinearLayoutManager.VERTICAL, true);
        adapter = new MsgAdapter(getActivity(), R.layout.item_msg, null);
        recyclerView.setAdapter(adapter);
        adapter.setClickListener(this);
//        adapter.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
//            @Override
//            public void onItemClick(int position, View v) {
//                if(position == 1){
//                    ((MainActivity)getActivity()).gotoOtherActivity(LeaveSpActivity.class);
//                }
//            }
//        });
    }

    @Override
    protected void initData() {
        bgaRefreshLayout.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(getActivity(),bgaRefreshLayout);
        bgaRefreshLayout.beginRefreshing();
//        list.clear();
//        for (int i = 0; i < titles.length; i++) {
//            MsgBean bean = new MsgBean();
//            bean.title = titles[i];
//            bean.content = contents[i];
//            list.add(bean);
//        }
    }

    @OnClick({R.id.fl_search})
    void click(View view) {
        switch (view.getId()) {
            case R.id.fl_search:
                fl_search.setVisibility(View.GONE);
                ((BaseActivity) getActivity()).showKeyboard(et_search);
                break;
        }
    }

    private void initSearch() {
        /**
         * 监听搜索键  设置 inputtype=text  singline=true
         */
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    ((BaseActivity) getActivity()).CloseKeyboard();
                    return true;
                }
                return false;
            }
        });
    }


    @Override
    public void onItemClick(int position, View v) {
        MyMessageBean bean = msgLists.get(position);
        int type = bean.getType();
        if(type == 1){ //1 打卡通知
            ((MainActivity)getActivity()).gotoOtherActivity(CardDetailActivity.class); //打卡记录列表
        }else if(type == 2){ //2请假审批(请假)
            ((MainActivity)getActivity()).gotoOtherActivity(LeaveActivity.class); //请假人
        }else if(type == 3){ //3企业通知
            ((MainActivity)getActivity()).gotoOtherActivity(QytzActivity.class); //企业通知
        }else if(type == 4){//4请假审批(审批)
            ((MainActivity)getActivity()).gotoFlagDataOtherActivity(LeaveSpActivity.class,bean,1); //审批人
//            ((MainActivity)getActivity()).gotoOtherActivity(LeaveSpActivity.class); //审批人
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse, tag);
        bgaRefreshLayout.endRefreshing();
        if(1 == tag){
            msgLists = apiResponse.data.list;
            adapter.setData(msgLists);
        }
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        super.OnErrorData(code, tag);
        bgaRefreshLayout.endRefreshing();
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error, tag);
        bgaRefreshLayout.endRefreshing();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        WebServiceApi.getInstance().messageList(SharedPreferencesUtils.getInstance().getString("token"),this,1);
    }
}
